<?php
namespace app\admin\controller;
use app\logic\DataHelper;
use app\logic\MenuHelper;
use think\facade\Db;
use think\facade\View;

class WxGzh extends Base
{
    public function index() {
        return View::fetch('index');
    }

    public function save() {
        $params = get_all_params();

        if (!check_params_empty(['title'])) {
            return json([ 'code' => -1, 'msg' => '请填写：title' ]);
        }
        if (isset($params["id"]) && $params["id"] > 0) {
            DataHelper::update("tb_wx_gzh", "id=".$params["id"], $params);
        } else {
            DataHelper::add("tb_wx_gzh", $params);
        }
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function edit($id = 0) {
        if ($id > 0) {
            $entry = DataHelper::find_by_id("tb_wx_gzh", $id);
        } else {
            $entry = [
                'id' => '0',
                'title' => '',
                'app_key' => '',
                'app_secret' => '',
                'status' => '',
            ];
        }
        return View::fetch('edit', [
            "entry" => $entry
        ]);
    }

    public function list($title = "", $page = 1, $limit = 20) {
        $query = " status=0 ";
        if (!empty($title)) {
            $query .= " and title like '%$title%' ";
        }

        list($list, $count) = DataHelper::select_page("tb_wx_gzh", $query, [ 'page' => $page, 'limit' => $limit ]);
        if ($count <= 0) {
            return json([ 'code' => 201, 'msg' => '没有相关数据' ]);
        }
        return json([ 'code' => 0, 'msg' => '成功', 'count' => $count, 'data' => $list ]);
    }

    public function del() {
        $params = get_all_params();
        $ids = $params['ids'];
        DataHelper::update('tb_wx_gzh', "id in ($ids)", [
            'status' => -1
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }
}
